package entities.bosses;

import it.randomtower.engine.entity.Entity;

import org.newdawn.slick.geom.Vector2f;

import utils.ResourceManager;
import entities.base.AbstractEnemy;
import entities.base.AbstractPlayer;

public class ManOfSteel extends AbstractEnemy {

	private int frame = 0;
	private int frameTimer = 0;
	private int frameInterval = 100;

	private boolean punching = false;

	public ManOfSteel(float x, float y) {
		super(x, y);

		resetState();

		setMaxHealth(100);

		gravity = 0.4f;
		maxSpeed = new Vector2f(1.6f, 10);
		friction = new Vector2f(0.5f, 0.5f);

		setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
	}

	/**
	 * Wenn der Spieler in Sicht ist, wird versucht, den Spieler zu schlagen und
	 * somit zu verletzen.
	 */
	private void startPunch() {
		if (punching)
			return;

		punching = true;
		sheet = ResourceManager.getSpriteSheet("boss1punch1");
		frame = 0;
		frameTimer = frameInterval;
		autoWalk = false;
		if (faceRight)
			currentImage = sheet.getSubImage(0, 0);
		else
			currentImage = sheet.getSubImage(0, 0).getFlippedCopy(true, false);

		currentImage = sheet.getSubImage(0, 0);
	}

	private void resetState() {
		sheet = ResourceManager.getSpriteSheet("boss1");
		if (faceRight)
			currentImage = ResourceManager.getImage("boss1Idle");
		else
			currentImage = ResourceManager.getImage("boss1Idle").getFlippedCopy(true, false);
	}

	@Override
	public void collisionResponse(Entity other) {
		if (punching) {
			if (other instanceof AbstractPlayer) {
				AbstractPlayer player = (AbstractPlayer) other;
				if (player.isRecentlyHit())
					return;
				player.setMaxHitTimer(1000);
				player.setRecentlyHit(true);
				player.modifyHealth(-30);
				if (faceRight) {
					player.speed.x = 15f;
					player.speed.y = -15f;
				} else {
					player.speed.x = -15f;
					player.speed.y = -15f;
				}
				player.motion(true, true);
			}
		}
	}

	@Override
	public void updateAI(int delta) {
		if (autoWalk) {
			if (faceRight)
				acceleration.x = 1;
			else
				acceleration.x = -1;
		}

		detectNextStep();

		gravity(delta);
		maxspeed(true, true);

		if (speed.x != 0) {
			frameTimer -= delta;
			while (frameTimer < 0) {
				frame++;
				frameTimer += frameInterval;
				if (frame > 7) {
					frame = 0;
					if (punching) {
						punching = false;
						resetState();
					}
				}
			}
		}

		if (speed.x < 0) {
			currentImage = sheet.getSprite(frame, 0).getFlippedCopy(true, false);
		}
		if (speed.x > 0) {
			currentImage = sheet.getSprite(frame, 0);
		}
		if (speed.x == 0) {
			if (faceRight) {
				currentImage = ResourceManager.getImage("boss1Idle");
			} else {
				currentImage = ResourceManager.getImage("boss1Idle").getFlippedCopy(true, false);
			}
		}

		if (!punching) {
			if (collide(Entity.PLAYER, x + 5, y) != null || collide(Entity.PLAYER, x - 5, y) != null)
				startPunch();
			else if (collide(Entity.PLAYER, x, y - 5) != null)
				System.out.println("ENEMEY: punch nach oben()");
		} else {
			Entity e = null;
			if ((e = collide(Entity.PLAYER, x + 5, y)) != null || (e = collide(Entity.PLAYER, x - 5, y)) != null)
				collisionResponse(e);
		}
	}

}
